package com.example.demo.service;

import com.example.demo.config.EmailConfig;
import com.example.demo.config.VelocityEngineBean;
import com.example.demo.service.interf.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.velocity.VelocityEngineUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by hongzhenyue on 18/2/27.
 */
@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private EmailConfig emailConfig;

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private VelocityEngineBean velocityEngine;

    @Override
    public void sendSimpleMail(String sendTo, String title, String content) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setSentDate(new Date());
        simpleMailMessage.setFrom(emailConfig.getUsername());
        simpleMailMessage.setTo(sendTo);
        simpleMailMessage.setSubject(title);
        simpleMailMessage.setText(content);
        mailSender.send(simpleMailMessage);
    }

    @Override
    public void sendAttachmentsMail(String sendTo, String titel, String content, List<File> targetFile) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();

        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(emailConfig.getUsername());
            helper.setTo(sendTo);
            helper.setSubject(titel);
            helper.setText(content, true);

            if (targetFile.size() > 0) {
                for (File fileItem : targetFile) {
                    FileSystemResource file = new FileSystemResource(fileItem);
                    helper.addAttachment(fileItem.getName(), file);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        mailSender.send(mimeMessage);
    }

    @Override
    public void sendTemplateMail(String sendTo, String titel, String content, List<File> targetFile) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(emailConfig.getUsername());
            helper.setTo(sendTo);
            helper.setSubject(titel);
            helper.setText(content);
            Map<String, Object> model = new HashMap();
            model.put("username", sendTo);
            String text = VelocityEngineUtils.mergeTemplateIntoString(
                    velocityEngine, "./src/main/resources/templates/template.vm", "UTF-8", model);
            helper.setText(text, true);

            if (targetFile.size() > 0) {
                for (File fileItem : targetFile) {
                    FileSystemResource file = new FileSystemResource(fileItem);
                    helper.addAttachment(fileItem.getName(), file);
                }
            }
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
